package WriteTool;


import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class TextEditor {
    public static void main(String[] args) {
        // 创建 JFrame 窗口
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 400);


        // 创建 JTextArea
        JTextArea textArea = new JTextArea("开始编辑");
        textArea.setFont(new Font("SansSerif", Font.PLAIN, 20)); // 初始字体大小
        JScrollPane scrollPane = new JScrollPane(textArea);

        // 创建上方的放大按钮面板
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton zoomInButton1 = new JButton("小号");
        JButton zoomInButton2 = new JButton("中号");
        JButton zoomInButton3 = new JButton("大号");
        topPanel.add(zoomInButton1);
        topPanel.add(zoomInButton2);
        topPanel.add(zoomInButton3);

        // 创建下方的颜色按钮和滑块面板
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton yellowButton = new JButton("黄色");
        JButton blueButton = new JButton("蓝色");
        JButton redButton = new JButton("红色");
        bottomPanel.add(yellowButton);
        bottomPanel.add(blueButton);
        bottomPanel.add(redButton);
        JSlider zoomSlider = new JSlider(JSlider.HORIZONTAL, 10, 70, 12);
        bottomPanel.add(zoomSlider);

        // 将面板添加到窗口
        frame.add(topPanel, BorderLayout.NORTH);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.add(bottomPanel, BorderLayout.SOUTH);

        // 为放大按钮添加事件监听器
        ActionListener zoomInListener = new ActionListener() {
            int zoomLevel = 1;

            @Override
            public void actionPerformed(ActionEvent e) {
                int fontSize = textArea.getFont().getSize() + zoomLevel;
                textArea.setFont(new Font("SansSerif", Font.PLAIN, Math.min(fontSize, 40)));
            }
        };

        zoomInButton1.addActionListener(zoomInListener);
        zoomInButton2.addActionListener(zoomInListener);
        zoomInButton3.addActionListener(zoomInListener);

        // 为颜色按钮添加事件监听器
        ActionListener colorListener = new ActionListener() {
            @Override
            //setForeground 方法用于设置组件文本的颜色，
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == yellowButton) {
                    textArea.setForeground(Color.YELLOW);
                } else if (e.getSource() == blueButton) {
                    textArea.setForeground(Color.BLUE);
                } else if (e.getSource() == redButton) {
                    textArea.setForeground(Color.RED);
                }
            }
        };

        yellowButton.addActionListener(colorListener);
        blueButton.addActionListener(colorListener);
        redButton.addActionListener(colorListener);

        // 为滑块添加事件监听器
        zoomSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int fontSize = zoomSlider.getValue();
                textArea.setFont(new Font("SansSerif", Font.PLAIN, fontSize));
            }
        });



        // 显示窗口
        frame.setVisible(true);
    }
}